﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace qly
{
    /// <summary>
    /// Interaction logic for HoaDonWindow.xaml
    /// </summary>
    public partial class HoaDonWindow : Window
    {
        HangHoa hanghoa = new HangHoa();
        Hoadon hoadon = new Hoadon();
        DataRowView datarow;
        bool addnew = true;
        string shd;
        public HoaDonWindow()
        {
            InitializeComponent();
        }
        public HoaDonWindow(DataRowView datarow, bool addnew)
            : this()
        {
            this.datarow = datarow;
            this.addnew = addnew;
            if (addnew == false)
            {
                shd = datarow["sohd"].ToString();
                txtSoHD.Text = shd;
                cboHangHoa.SelectedValue = datarow["tenhang"];
                txtDienGiai.Text = datarow["diengiai"].ToString();
                txtSoLuong.Text = datarow["soluong"].ToString();
                txtDonGia.Text = datarow["dongia"].ToString();
            }

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            DataTable dt = hanghoa.DanhSachHangHoa();
            cboHangHoa.ItemsSource = dt.DefaultView;
            cboHangHoa.DisplayMemberPath = "tenhang";
            cboHangHoa.SelectedValuePath = "mahang";


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Hoadon b = new Hoadon();
            string sohd = txtSoHD.Text;
            string mahh = cboHangHoa.SelectedValue.ToString();
            string tenhang = cboHangHoa.Text;
            string dg = txtDienGiai.Text;
            double sl = double.Parse(txtSoLuong.Text);
            double dongia = double.Parse(txtDonGia.Text);
            double trigia = sl * dongia;
            double khuyenmai = 0.0;
            string shd = this.shd;
            if (trigia > 20000000)
                khuyenmai = (trigia * 5) / 100;
            else
                khuyenmai = 0.0;
            if (addnew == true)
            {
                b.LuuHoaDon(sohd, mahh, dg, sl, dongia);
            }
            else
            {
                b.SuaHoaDon(sohd, mahh, dg, sl, dongia);
            }

            datarow["sohd"] = sohd;
            datarow["tenhang"] = tenhang;
            datarow["soluong"] = sl;
            datarow["dongia"] = dongia;
            datarow["trigia"] = trigia;
            datarow["khuyenmai"] = khuyenmai;
            datarow["diengiai"] = dg;
            this.Close();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
